package com.link.play.fit.ai.ui.guide

import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.link.play.fit.ai.databinding.FgCalAppBinding
import com.link.play.fit.ai.ui.guide.ac.GuidePbAc
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch


class CalAppFg : BasePageFragment<FgCalAppBinding>(FgCalAppBinding::inflate) {


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val userGuideState = (requireActivity() as GuidePbAc).viewModel.userGuideState
        lifecycleScope.launch {
            userGuideState.collectLatest {
                when (it.calApp) {
                    0 -> {
                        binding.llNo.isSelected = true
                        binding.llYes.isSelected = false
                    }

                    1 -> {
                        binding.llYes.isSelected = true
                        binding.llNo.isSelected = false
                    }
                }
            }
        }

        binding.llNo.setOnClickListener {
            userGuideState?.update {
                it.copy(calApp = 0)
            }
        }
        binding.llYes.setOnClickListener {
            userGuideState?.update {
                it.copy(calApp = 1)
            }
        }
    }

}